package com.jdy.haoduoai.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;

import com.jdy.haoduoai.LeXiaoXiaoBanApp;
import com.jdy.haoduoai.intface.IAsyncTask;
import com.jdy.haoduoai.model.LeMachine;
import com.jdy.haoduoai.model.LeUser;
import com.jdy.haoduoai.parse.JsonSerializer;
import com.jdy.haoduoai.rx.util.async.Async;
import com.jdy.haoduoai.util.CustomAsyncTask;
import com.jdy.haoduoai.util.HttpUtils;
import com.jdy.haoduoai.util.LeConfig;
import com.jdy.haoduoai.util.ResponseResult;
import com.jdy.haoduoai.util.Tools;
import com.jdy.haoduoai.view.RippleView;
import com.jdy.haoduoai.view.RippleView.OnRippleCompleteListener;
import com.ycsj.goldmedalnewconcept.R;

import org.droidparts.annotation.inject.InjectView;

import java.util.HashMap;

import rx.functions.Action1;
import rx.functions.Func0;
import rx.functions.Func1;

//import com.jdy.haoduoai.R;

/**
 * Input activation activity.
 * 
 * @author zp @ 2015-6-12 下午4:04:43
 */
public class InputActivationActivity extends BaseActivity {

  @InjectView(id = R.id.submitRippleView)
  private RippleView mSubmitRippleView;
  @InjectView(id = R.id.editText1)
  private EditText mEditText1;
  @InjectView(id = R.id.editText2)
  private EditText mEditText2;
  @InjectView(id = R.id.editText3)
  private EditText mEditText3;

  private LeUser mLeUser;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.input_activation_layout);

    setTitle_BgColor(0xff7ed2f5);
    setTitle("使用服务卡");
    mLeUser = LeXiaoXiaoBanApp.getInstance().getUser();
    initView();
  }

  private void getExpiredDate(final LeUser lu) {
    Async.start(new Func0<ResponseResult>() {
      @Override
      public ResponseResult call() {

        HashMap<String, Object> param_ = new HashMap<String, Object>();
        param_.put(HttpUtils.TAG_SNO_I, lu.sno);
        param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);

        return HttpUtils.startRequest(HttpUtils.API_URL
            + HttpUtils.GET_DEVICE_DETAIL_GET + lu.machine.id, param_,
            HttpUtils.TAG_GET_REQUEST);
      }
    }).filter(new Func1<ResponseResult, Boolean>() {
      @Override
      public Boolean call(ResponseResult arg0) {
        return arg0 != null && Tools.isNotEmpty(arg0) && arg0.isSuccess();
      }
    }).map(new Func1<ResponseResult, String>() {
      @Override
      public String call(ResponseResult arg0) {
        return arg0.data;
      }
    }).subscribe(new Action1<String>() {
      @Override
      public void call(String arg0) {

        if (Tools.isNotEmpty(arg0)) {
          LeMachine macInfo = JsonSerializer.getInstance().deserialize(arg0,
              LeMachine.class);

          if (macInfo != null && macInfo.activate != null) {
            LeConfig.isDeviceActivated = !macInfo.activate.is_expired;
            LeConfig.expiredAt = macInfo.activate.expired_at;
          }
        }

      }
    });
  }

  private void initView() {
    mSubmitRippleView
        .setOnRippleCompleteListener(new OnRippleCompleteListener() {
          @Override
          public void onComplete(RippleView rippleView) {
            if (Tools.isEmpty(mEditText1.getText().toString().trim())
                || Tools.isEmpty(mEditText2.getText().toString())
                || Tools.isEmpty(mEditText3.getText().toString())) {
              toastShow("请输入完整年费码");
              return;
            } else {
              final String serialNo = mEditText1.getText().toString() + "-"
                  + mEditText2.getText().toString() + "-"
                  + mEditText3.getText().toString();

              showloading("");

              new CustomAsyncTask(InputActivationActivity.this,
                  new IAsyncTask() {
                    @Override
                    public void onRecieveData(Activity activity,
                        ResponseResult result) {
                      dismissLoading();
                      toastShow(result.data);
                      if (result.isSuccess()) {
                        if (mLeUser == null) {
                          mLeUser = Tools.restoreLeUser();
                        }
                        getExpiredDate(mLeUser);
                        LeConfig.isDeviceActivated = true;
                        // startActivity(new
                        // Intent(InputActivationActivity.this,
                        // InputBabyActivity.class));

                        Intent intent = new Intent(
                            InputActivationActivity.this, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                      }
                      overridePendingTransition(R.anim.push_left_in,
                          R.anim.push_left_out);
                    }

                    @Override
                    public ResponseResult doInbackground(Activity activity) {

                      HashMap<String, Object> param_ = new HashMap<String, Object>();

                      param_.put(HttpUtils.TAG_SNO_I, mLeUser.sno);
                      param_.put(HttpUtils.TAG_MAC_ID_I, mLeUser.machine.id);
                      param_.put(HttpUtils.TAG_ACTIVATION_NO_I, serialNo);
                      param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);

                      return HttpUtils.startRequest(HttpUtils.API_URL
                          + HttpUtils.ACTIVATE_DEVICE_METHOD_POST, param_,
                          HttpUtils.TAG_POST_REQUEST);
                    }
                  }).execute();
            }
          }
        });
  }
}
